<template>
  <div class="page-container">
    <page_breadcrumb
      :getKeyMethod="init_table_model"
      @change="change_table_model"
      pageOption
    />
    <!--  -->

    <ex_super_table_page
      ref="SuperTablePage"
      v-bind="option"
      @selection-change="(e) => (selection = e)"
      @search="searchChange"
    >
    </ex_super_table_page>

    <ex_super_dialog_form
      ref="catalogueRef"
      v-bind="accept_dialog"
      @afterSubmitSuccess="$refs['SuperTablePage'].reload()"
      :noSubmit="noSubmit"
    >
    </ex_super_dialog_form>

    <placeImport ref="importRef" @submitSuccess="$refs['SuperTablePage'].reload()" />
  </div>
</template>
<script>
import placeImport from "./dialog/place_import.vue";
import business_function from "@/common/business_function";
import page_table_model_manage from "@/mixin/page_table_model_manage";
import superTablePageFunction from "@/mixin/super-table-page-function";
const ch_num = [
  "一",
  "二",
  "三",
  "四",
  "五",
  "六",
  "七",
  "八",
  "九",
  "十",
  "十一",
  "十二",
  "十三",
  "十四",
  "十五",
];
export default {
  title: "label_workorder_catalogue_list",
  mixins: [superTablePageFunction, page_table_model_manage],
  components: { placeImport },
  data() {
    return {
      selection: [],

      option: {
        search: {
          default: { needRootCatalog: "Y" },
          model: [
            // { key: 'status', label: '异常状态', type: 'select', enum: 'AbnormalStatusEnum', },
            {
              key: "id",
              label: "工单目录",
              // type: "workorder_catalogue_select",
              render: (h, { data }) => {
                return h("workorder_catalogue_cascader", {
                  class: "form_input_default",
                  props: {
                    value: data.ticketsCatalogId,
                    needRootCatalog: "N",
                    // disabled: this.checkinFormDisabled("customerId"),
                  },
                  on: {
                    changeItem: (e) => {
                      this.$set(data, "ticketsCatalogId", e.id);
                    },
                  },
                });
              },
            },
            {
              key: "scenario",
              label: "场景解析",
              // type: "common_customer_choose",
            },
            {
              key: "status",
              label: "状态",
              type: "select",
              enum: "IsAvailableStatusEnum",
              // items: [
              //   { value: 1, label: "启用" },
              //   { value: 2, label: "停用" },
              // ],
            },
          ],
        },
        actionExtra: [
          {
            label: "label_add_catalogue",
            auth: "oms_catalogue_add",
            attrs: { icon: "el-icon-plus", type: "primary" },
            click: () => {
              this.up_id = undefined;
              this.up_name = undefined;
              this.up_type = undefined;
              this.accept_dialog.title = "新增工单目录";
              this.accept_dialog.form.disabled = false;
              this.accept_dialog.submitUrl =
                "/oms-api/tickets/OmsBaseTicketsCatalogRpc/save.json";
              this["$refs"]["catalogueRef"].show();
              this.clearCatalogueFrom();
            },
          },
          {
            // label: "导入",
            label: "label_catalogue_import",
            auth: "oms_catalogue_import",
            attrs: { icon: "el-icon-circle-check", type: "primary" },
            click: this.importFn,
          },
          {
            // label: "停用",
            label: "label_catalogue_disable",
            auth: "oms_catalogue_disable",
            attrs: { icon: "el-icon-circle-close", type: "primary" },
            click: () => {
              if (_.isEmpty(this.selection)) return this.$message.error("请选择工单目录");
              this.onEnableCatalogue(this.selection, 1);
            },
          },
          {
            // label: "启用",
            label: "label_catalogue_enable",
            auth: "oms_catalogue_enable",
            attrs: { icon: "el-icon-plus", type: "primary" },
            click: () => {
              if (_.isEmpty(this.selection)) return this.$message.error("请选择工单目录");
              this.onEnableCatalogue(this.selection, 2);
            },
          },
          // {
          //   label: "删除",
          //   attrs: { icon: "el-icon-plus", type: "primary" },
          //   click: () => {
          //     if (_.isEmpty(this.selection)) return this.$message.error("请选择工单目录");
          //     this.onDeleteCatalogue(this.selection);
          //   },
          // },
          {
            // label: "导出",
            label: "label_catalogue_export",
            auth: "oms_catalogue_export",
            attrs: { icon: "el-icon-plus", type: "primary" },
            click: this.exportExcel,
          },
        ],
        table: {
          url: "/oms-api/tickets/OmsBaseTicketsCatalogRpc/getTicketsCatalogs.json",
          model: this.init_table_model(),
        },
      },
      up_id: undefined,
      up_name: undefined,
      up_type: undefined,
      noSubmit: true,
      accept_dialog: {
        title: "新增工单目录",
        customClass: this.up_id ? "dialog_auto" : "dialog_auto  hide-footer-btn",
        submitUrl: "",
        detailUrl: "/oms-api/tickets/OmsBaseTicketsCatalogRpc/getTicketsCatalogById.json",
        form: {
          inline: true,
          disabled: false,
          itemClass: "form_item_default",
          inputClass: "form_input_default",
          formClass: "form_default_col2 flex_row_wrap",
          default: {
            type: "QUESTION",
            fatherId: this.up_id,
          },
          model: [
            // { header: "新增工单目录" },
            // {
            //   key: "customerName",
            //   label: "label_customer",
            //   formType: "text",
            //   formatter: (r, c, v, i) => this.$formatNameCode(v, r.customerCode),
            // },
            {
              key: "type",
              label: "目录类型",
              type: "select",
              enum: "TicketsTypeEnum",
              disabled: (data) => {
                if (this.up_type) {
                  this.$set(data, "type", this.up_type);
                  return true;
                }
                return false;
              },
              rules: {
                required: true,
                message: "请选择目录类型",
                trigger: "change",
              },
            },

            {
              key: this.up_name ? "fatherName" : "fatherId",
              label: `上级目录`,
              rules: {
                required: true,
                message: "请选择上级目录",
                trigger: "change",
              },
              // workorder_catalogue_cascader / workorder_catalogue_select
              render: (h, { data }) => {
                if (this.up_id) {
                  this.$set(data, "fatherId", this.up_id);
                  this.$set(data, "fatherName", this.up_name);
                  return h("workorder_catalogue_select", {
                    class: "form_input_default",
                    props: {
                      value: this.up_name || this.up_id,
                      needRootCatalog: "Y",
                      disabled: true,
                    },
                  });
                }
                return h("workorder_catalogue_cascader", {
                  class: "form_input_default",
                  props: {
                    value: data.fatherId,
                    needRootCatalog: "Y",
                  },
                  on: {
                    changeItem: (e) => {
                      this.$set(data, "fatherId", e.id);
                      this.$set(data, "fatherName", e.name);
                    },
                  },
                });
              },
              // if: () => {
              //   return this.up_id ? false : true;
              // },
            },
            {
              key: "name",
              label: "目录名称",
              type: "input",
              rules: {
                required: true,
                message: "请输入目录名称",
                trigger: "blur",
              },
            },
            {
              key: "scenario",
              label: "场景解析",
              type: "input",
              rules: {
                required: true,
                message: "请输入场景解析",
                trigger: "blur",
              },
            },
            {
              key: "userDeptIds",
              label: "处理部门",
              rules: {
                required: true,
                message: "请选择处理部门",
                trigger: "change",
              },
              render: (h, { data }) => {
                return h("dept_select_cascader", {
                  class: "form_input_default",
                  props: {
                    value: data.userDeptIds,
                  },
                  on: {
                    changeItem: (e) => {
                      console.log("changeItem 888", e);
                      this.$set(data, "userDeptIds", e.id);
                      this.$set(data, "userDeptName", e.name);
                    },
                  },
                });
              },
              // formatter: (r, c, v, i) => this.$enumShow(v, "AnomalyCauseEnum"),
            },
            {
              key: "userRoleName",
              label: "处理角色",
              rules: {
                required: true,
                message: "请选择处理角色",
                trigger: "change",
              },
              render: (h, { data }) => {
                return h("role_select", {
                  class: "form_input_default",
                  // urlParams: { roleSystem: "OMS" },
                  props: {
                    // roleSystem: "OMS",
                    value: data.userRoleName,
                    // disabled: this.checkinFormDisabled("customerId"),
                  },
                  on: {
                    changeItem: (e) => {
                      this.$set(data, "userRoleId", e.id);
                      this.$set(data, "userRoleName", e.name);
                    },
                  },
                });
              },
            },
            {
              key: "handleTimeValue",
              label: "标准处理时效",
              type: "input-number",
              attrs: {
                "step-strictly": true,
                min: 0,
              },
              rules: {
                required: true,
                message: "请输入标准处理时效",
                trigger: "blur",
              },
            },
            {
              key: "handleTimeUnit",
              label: "标准处理时效单位",
              type: "select",
              enum: "PrescriptionUnitEnum",
              rules: {
                required: true,
                message: "请选择标准处理时效单位",
                trigger: "change",
              },
            },
            {
              key: "transferTimeValue",
              label: "流转环节时效",
              type: "input-number",
              attrs: {
                "step-strictly": true,
                min: 0,
              },
              rules: {
                required: true,
                message: "请输入流转环节时效",
                trigger: "blur",
              },
            },
            {
              key: "transferTimeUnit",
              label: "流转环节时效单位",
              type: "select",
              enum: "PrescriptionUnitEnum",
              rules: {
                required: true,
                message: "请选择流转环节时效单位",
                trigger: "change",
              },
            },
            {
              key: "roleIdLevel1",
              label: "一级升级角色",
              rules: {
                required: true,
                message: "请选择一级升级角色",
                trigger: "change",
              },
              render: (h, { data }) => {
                return h("role_select", {
                  class: "form_input_default",
                  urlParams: { roleSystem: "OMS" },
                  props: {
                    value: data.roleNameLevel1,
                    // value: data.customerId,
                    // disabled: this.checkinFormDisabled("customerId"),
                  },
                  on: {
                    changeItem: (e) => {
                      this.$set(data, "roleIdLevel1", e.id);
                      this.$set(data, "roleNameLevel1", e.name);
                    },
                  },
                });
              },
              // formatter: (r, c, v, i) => this.$enumShow(v, "AnomalyCauseEnum"),
            },
            {
              key: "roleIdLevel2",
              label: "二级升级角色",
              rules: {
                required: true,
                message: "请选择二级升级角色",
                trigger: "change",
              },
              render: (h, { data }) => {
                return h("role_select", {
                  class: "form_input_default",
                  urlParams: { roleSystem: "OMS" },
                  props: {
                    value: data.roleNameLevel2,
                    // value: data.customerId,
                    // disabled: this.checkinFormDisabled("customerId"),
                  },
                  on: {
                    changeItem: (e) => {
                      this.$set(data, "roleIdLevel2", e.id);
                      this.$set(data, "roleNameLevel2", e.name);
                    },
                  },
                });
              },
              // formatter: (r, c, v, i) => this.$enumShow(v, "AnomalyCauseEnum"),
            },
            {
              key: "remark",
              label: "目录备注",
              // formatter: (r, c, v, i) => this.$dateFormat(v),
            },
          ],
        },
      },
    };
  },
  created() {
    // this.searchChange();
  }, 
  methods: {
    option_table_model() {
      return [
        { type: "selection", clickToggle: true },
        {
          key: "name",
          label: "工单目录",
          // minWidth: 120,
          "show-overflow-tooltip": true,
        },
        {
          key: "catalogLevel",
          label: "目录级别",
          // width: 1200,
          "show-overflow-tooltip": true,
        },
        {
          key: "scenario",
          label: "场景解析",
          // width: 120,
          "show-overflow-tooltip": true,
        },
        {
          key: "status",
          label: "状态",
          // width: 120,
          "show-overflow-tooltip": true,
          formatter: (r, c, v, i) => {
            if (r.status === "ENABLE") return "启用";
            if (r.status === "DISABLED") return "停用";
            return "";
          },
          // this.$localEnum.PutawayTypeEnum,
          // formatter: (r, c, v, i) => this.$enumShow(v, "IsAvailableStatusEnum"),
        },
        {
          key: "userDeptName",
          label: "处理部门",
          // minWidth: 120,
          "show-overflow-tooltip": true,
        },
        {
          key: "userRoleName",
          label: "对接角色",
          // width: 120,
        },
        {
          key: "handleTimeUnit",
          label: "标准处理时效",
          // width: 120,
          formatter: (r, c, v, i) => {
            if (r.handleTimeUnit === "HOUR") return `${r.handleTimeValue}小时`;
            if (r.handleTimeUnit === "DAY") return `${r.handleTimeValue}天`;
            if (r.handleTimeUnit === "WORK_DAY") return `${r.handleTimeValue}工作日`;
            return r.handleTimeValue;
          },
        },
        {
          type: "operate",
          width: 210,
          operate: [
            // {
            //   label: "复核",

            //   click: (row) => {},
            // },

            {
              type: "more",
              items: [
                {
                  label: "label_view_detail",
                  auth: "oms_catalogue_detail",
                  click: (row) => {
                    this.accept_dialog.title = "查看目录";
                    this.up_id = row.fatherId;
                    this.up_name = row.fatherName;
                    this.up_type = undefined;
                    this.accept_dialog.form.disabled = true;
                    this.clearCatalogueFrom();
                    this.noSubmit = true;
                    this["$refs"]["catalogueRef"].show({ id: row.id });
                  },
                },

                {
                  // label: "编辑",
                  label: "label_view_redact",
                  auth: "oms_catalogue_redact",
                  if: (row) => row.catalogLevel !== 1,
                  click: (row) => {
                    this.accept_dialog.title = "编辑目录";
                    this.accept_dialog.submitUrl =
                      "/oms-api/tickets/OmsBaseTicketsCatalogRpc/update.json";
                    this.up_id = row.fatherId;
                    this.up_name = row.fatherName;
                    this.up_type = undefined;
                    this.accept_dialog.form.disabled = false;
                    this.clearCatalogueFrom();
                    this["$refs"]["catalogueRef"].show({ id: row.id });
                  },
                },
                {
                  // label: "停用",
                  label: "label_catalogue_disable",
                  auth: "oms_catalogue_disable",
                  if: (row) => row.status === "ENABLE" && row.catalogLevel !== 1,
                  click: (row) => {
                    this.onEnableCatalogue([row], 1);
                  },
                },
                {
                  // label: "启用",
                  label: "label_catalogue_enable",
                  auth: "oms_catalogue_enable",
                  if: (row) => row.status === "DISABLED" && row.catalogLevel !== 1,
                  click: (row) => {
                    this.onEnableCatalogue([row], 2);
                  },
                },
                {
                  // label: "删除",
                  label: "label_catalogue_delete",
                  auth: "oms_catalogue_delete",
                  if: (row) => row.status === "DISABLED" && row.catalogLevel !== 1,
                  click: (row) => this.onDeleteCatalogue([row]),
                },
                {
                  label: (row) => {
                    // const t = {
                    //   _key: "label_add_level_catalogue",
                    //   type: ch_num[row.catalogLevel] || "",
                    // };
                    // const t = this.$tt({
                    //   _key: "label_add_level_catalogue",
                    //   i18n: true,
                    //   type: ch_num[row.catalogLevel] || "",
                    // });
                    const t = `新增${ch_num[row.catalogLevel] || ""}级工单目录`;
                    return t;
                  },
                  auth: "oms_catalogue_level_add",
                  click: (row) => {
                    this.up_id = row.id;
                    this.up_name = row.name;
                    this.up_type = row.type;
                    this.accept_dialog.title = `新增${
                      ch_num[row.catalogLevel] || ""
                    }级工单目录`;
                    this.accept_dialog.submitUrl =
                      "/oms-api/tickets/OmsBaseTicketsCatalogRpc/save.json";
                    this.accept_dialog.form.disabled = false;
                    this.clearCatalogueFrom();
                    this["$refs"]["catalogueRef"].show();
                  },
                },
              ],
            },
          ],
        },
      ];
    },
    clearCatalogueFrom() {
      this.noSubmit = false;
      try {
        this.$nextTick(() => {
          this.$refs["catalogueRef"].clearValidate();
        });
      } catch (e) {}
    },
    importFn() {
      this.$refs.importRef.show();
    },
    exportExcel() {
      business_function.confirmOperateBaseSelectOrForm.call(this, {
        data: this.$refs.SuperTablePage.getSearchFormData(),
        model: this.option.search.model,
        selection: this.selection,
        callback: (selection, data) => {
          let params = {};
          if (!_.isEmpty(selection)) {
            params = {
              ids: selection.map((it) => it.id),
            };
          } else {
            params = data;
          }
          this.$functionCommon({
            url: "/oms-api/tickets/OmsBaseTicketsCatalogRpc/export.json",
            data: params,
            type: "form",
          });
        },
      });
    },
    // 启用停用
    onEnableCatalogue(rows, type) {
      let url = null,
        title = null;
      if (type === 1) {
        url = "/oms-api/tickets/OmsBaseTicketsCatalogRpc/disable.json";
        title = "停用";
      } else {
        url = "/oms-api/tickets/OmsBaseTicketsCatalogRpc/enable.json";
        title = "启用";
      }
      const id_ = rows.map((it) => {
        return it.id;
      });
      this.$confirm(
        `将${title}选择的${rows.length}个目录，是否确认？`,
        // this.$tt("tip_xxx_will_comfirm_count_abnormal", { count: rows.length }),
        this.$tt("label_tip"),
        {
          type: "warning",
        }
      )
        .then(() => {
          this.$ajax({
            url: url,
            method: "post",
            type: "json",
            loading: true,
            params: {
              ids: id_.join(","),
            },
            success: (res) => {
              this.$message({
                showClose: true,
                message: this.$tt("tip_submit_success"),
                type: "success",
              });
              this.reload();
            },
          });
        })
        .catch(() => {});
    },
    onDeleteCatalogue(rows) {
      //
      const id_ = rows.map((it) => {
        return it.id;
      });
      this.$confirm(
        `将删除选择的${rows.length}个目录，是否确认？`,
        // this.$tt("tip_xxx_will_comfirm_count_abnormal", { count: rows.length }),
        this.$tt("label_tip"),
        {
          type: "warning",
        }
      )
        .then(() => {
          this.$ajax({
            url: "/oms-api/tickets/OmsBaseTicketsCatalogRpc/remove.json",
            method: "post",
            type: "json",
            loading: true,
            params: {
              id: id_.join(","),
            },
            success: (res) => {
              this.$message({
                showClose: true,
                message: this.$tt("tip_submit_success"),
                type: "success",
              });
              this.reload();
            },
          });
        })
        .catch(() => {});
    },

    searchChange(data) {
      return;
      this.$ajax({
        // url: "/ums-api/user/UserInfoRpc/login.json",
        url: "/oms-api/tickets/OmsBaseTicketsCatalogRpc/getTicketsCatalogs.json",
        method: "post",
        data: { id: 1767912072648790017 },
        success: (res) => {
          console.log("tag", res);
        },
      });
    },
  },
};
</script>
<style scoped>
.hide-footer-btn .dialog_button_footer {
  display: none !important;
}
</style>
